package communicationMessages;

import java.util.Vector;
import java.util.logging.Level;

import serverComuncation.Observer;
import logger.LoggerWriter;
import data.Forum;
import data.ForumMessage;
import data.MemberDataBase;
import data.SubForum;
import database.DB;

public class DeleteMsgMessage extends Message {

	@Override
	public String getAnswer() {
		String[] arr = this.content.split(delimiter);
		LoggerWriter logger = LoggerWriter.getLoggerWriter();
		String ans = "MsgDeleted"+delimiter;
		if (arr.length != 5)
		{
			logger.log("failed"+delimiter+"DeleteMsg Message wrong format"+delimiter, Level.WARNING);
			ans = "failed"+delimiter+"DeleteMsg Message wrong format"+delimiter;
		}
		else
		{
			String userName, subForumId , threadId , messageId,stringIsThread ;
			userName = arr[0];
			subForumId = arr[1];
			threadId = arr[2];
			messageId = arr[3];
			stringIsThread = arr[4];
			if (DB.isSubForumExist(subForumId) && DB.isThreadExist(threadId))
			{
				Vector<String> path = new Vector<String>();
				path.add(subForumId);
				path.add(threadId);
				boolean isThread = true;
				boolean success = false;
				isThread = addToPathPostMsg(threadId, messageId, path, isThread,stringIsThread);
				String userRoll = DB.getUserRoll(userName, path);
				if (userRoll.equals("admin")||userRoll.equals("owner")||
						(userRoll.equals("moderator") && DB.checkMoreThanAmonth(userName,subForumId)))
				{
					success= deleteMsg(threadId, messageId, isThread);
				}
				
				if (success)
				{
					ans += userRoll+delimiter+"Success"+delimiter;
				}
				else
				{
					ans += userRoll+delimiter+"Failed"+delimiter;
				}
				
			}
				
		}
		return ans;
	}

	private boolean addToPathPostMsg(String threadId, String messageId,
			Vector<String> path, boolean isThread,String stringIsThread) {
		if (stringIsThread.equals("false"))
		{
			isThread = false;
			path.add(messageId);
		}
		return isThread;
	}

	private boolean deleteMsg(String threadId, String messageId, boolean isThread) {
		boolean success =false;
		if (isThread)
		{
			DB.deleteThreadMessage(threadId);
			success = true;
		}
		else if (DB.isPostMsgExist(messageId))
		{
			DB.deletePostMessage(messageId);
			success = true;
		}
		return success;
	}
}
		
		
		
		
//
//		String userRoll = DB.getUserRoll(userName);
//		Vector<String> path = new Vector<String>();
//		path.add(subForumId);
//		boolean isSubForumExist = DB.isSubForumExist(subForumId);
//		boolean isThreadExist = DB.isThreadExist(threadId);
//		if (isSubForumExist && isThreadExist){
//			if (userRoll.equals("owner") || userRoll.equals("admin") || 
//					(userRoll.equals("moderator") && DB.checkMoreThanAmonth(userName,subForumId))){
//				if (!threadId.equals(messageId)){	// its a post message
//					DB.deletePostMessage(messageId);
//				}
//				else{// its a thread message
//					DB.deleteThreadMessage(threadId);
//				}	
//				ans += userRoll+delimiter+"Success"+delimiter;
//			}
//		}
//		else  ans += userRoll+delimiter+"Failed"+delimiter;
//
//
//	}
//	return ans;
//}
		
		
		
		
		

/*
 * 

 * String userName, subForumId , threadId , messageId ;
			userName = arr[0];
			subForumId = arr[1];
			threadId = arr[2];
			messageId = arr[3];
			int subForumInt = Integer.valueOf(subForumId);
			int threadInt = Integer.valueOf(threadId);;
			int messageInt = Integer.valueOf(messageId);
			Vector<Integer> path = new Vector<Integer>();
			path.add(subForumInt);
			Forum forum = Forum.getForum();
			SubForum subForum = forum.getSubForum(subForumInt);
			int userId = MemberDataBase.getMembersDataBase().getMember(userName).get_id();
			if (threadInt != messageInt)
			{
				if(subForum.deletePostMessage(userId, threadInt, messageInt)) 
					ans += MemberDataBase.getUserRoll(userName)+delimiter+"Success"+delimiter;
				else ans += MemberDataBase.getUserRoll(userName)+delimiter+"Failed"+delimiter;
			}
			else
			{
				if(subForum.deleteThreadMessage(userId, threadInt)) 
					ans += MemberDataBase.getUserRoll(userName)+delimiter+"Success"+delimiter;
				else ans += MemberDataBase.getUserRoll(userName)+delimiter+"Failed"+delimiter;
			}

		} 
		return ans;
	}
 */
